Imports System
Imports System.Xml

''CONVERTED

Public Class SpreadReqMsg
    Inherits BaseMsg

    'Protected m_secTyp As String
    '##Product Terms Request
    '<CPCAPI v="1.1">
    '      <ContractTermReq ReqID="testID" ProductID="LN"/>
    '</CPCAPI>

    '## Product Terms Response
    '<CPCAPI v="1.1">
    '      <ContractTermRsp ReqID="testID" ProductID="LN">
    '            <Term MMY="200706"/>
    '            <Term MMY="200707"/>

    Public Sub New()
        RequestType = "SecDefReq"
        ResponseType = "SecDef"
        ReqTyp = "100" ' indicates futures. for options, should be 102
    End Sub


    Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

        If m_prodid = "" Then
            Return False
        End If

        XmlW.WriteStartElement("Instrmt")
        XmlW.WriteAttributeString("ID", m_prodid)
        XmlW.WriteAttributeString("Src", "H")
        XmlW.WriteAttributeString("Exch", "NYMEX")

		XmlW.WriteAttributeString("SecTyp", m_secTyp)

        XmlW.WriteEndElement()

        Return True
    End Function


    'Protected Overrides Function InstrmtMsg(ByRef XmlW As XmlTextWriter) As Boolean

    '    If m_prodid = "" Then
    '        Return False
    '    End If

    '    XmlW.WriteStartElement("Instrmt")

    '    XmlW.WriteAttributeString("ID", m_prodid)
    '    XmlW.WriteAttributeString("Src", "H")
    '    XmlW.WriteAttributeString("Exch", "NYMEX")

    ''If (ReqTyp = "104") Then
    '    XmlW.WriteAttributeString("SecTyp", m_secTyp)
    ''Else
    ''XmlW.WriteAttributeString("SecTyp", "OOF")
    ''End If

    '    XmlW.WriteEndElement()
    '    Return True
    'End Function

    Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

        Dim prod As XmlNode
        'Dim attr As XmlAttribute

        Dim dbi As dbInterface = Globals.dbi

        'If node.Attributes("ProductID").InnerText = m_prodid Then

        'dbi.nonquery("delete from nymx_prod_terms where prod_id=" & dbi.quote(m_prodid))
        'find the SecDef node, then find the instrmt node inside
        For Each prod In node.ChildNodes
            If prod.Name = "Undly" Then
				m_undlyid = prod.Attributes("ID").InnerText.ToString()
				m_undly_sectyp = prod.Attributes("SecTyp").InnerText.ToString()
				m_secTyp = prod.Attributes("SecTyp").InnerText.ToString()
            End If
        Next
        'End If

    End Sub

    Private Sub InsertTerms(ByRef prod As XmlNode)
        '            <Term MMY="200706"/>
        Dim dbi As dbInterface = Globals.dbi
        Dim term As String
        Dim instr As XmlNode
        Dim child As XmlNode
        Dim matdt As String
        Dim mult As String
        Dim dtFirst As Date
        Dim dtLast As Date
        Dim dtLastEFP As Date
        Dim val As Date

        instr = prod.ChildNodes(0) '  should be Instrmt node

        term = getAttr(instr, "MMY")
        matdt = getAttr(instr, "MatDt")
        mult = getAttr(instr, "Mult")

        If prod.Name = "Term" Then
            term = prod.Attributes("MMY").InnerText
        End If

        Dim chd As XmlNode
        Dim att As String

        For Each chd In instr.ChildNodes
            att = getAttr(chd, "EventTyp")
            val = Date.Parse(getAttr(chd, "Dt"))
            If att = "5" Then
                dtFirst = val
            ElseIf att = "7" Then
                dtLast = val
            ElseIf att = "105" Then
                dtLastEFP = val
            End If

        Next

        dbi.SetupStoredProc("sp_nymx_ins_product_term")
        dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
        dbi.AddStoredProcParam("@term", SqlDbType.VarChar, term)
        'dbi.AddStoredProcParam("@maturity", SqlDbType.VarChar, matdt)
        'dbi.AddStoredProcParam("@multi", SqlDbType.VarChar, mult)
        'dbi.AddStoredProcParam("@dt_first_td", SqlDbType.DateTime, dtFirst)
        'dbi.AddStoredProcParam("@dt_last_td", SqlDbType.DateTime, dtLast)
        'dbi.AddStoredProcParam("@dt_last_efp", SqlDbType.DateTime, dtLastEFP)
        dbi.RunStoredProc()



    End Sub

End Class
